<?php
namespace H5\Controller;
use Think\Controller;

class WaiqinController extends CommonController {

	
	public function index(){
        $userid = session('userid');
        $level  = session('level');
        $map =array();
        if($level == 1) {  // 普通销售员身份
            $map['uid'] = $userid;
            
        }
        $map['sign_out_time'] = array('neq','');
        
		$list_arr = M('out')->where($map)->order('sign_in_time desc')->select();
		//print_r($list_arr);
		$this->assign('list',$list_arr);
        $this->assign('menu','waiqin'); // 外勤拜访
		$this->display();	
	}


	public function waiqin() {
		vendor('JSSDK.jssdk');
		$appid = C('QYWX.appid');
		$secret = C('QYWX.appsecret');
		$sign = new \JSSDK($appid, $secret);
		$signPackage = $sign -> GetSignPackage();
		$this -> assign('sign', $signPackage);
        $this -> assign('menu','waiqin'); // 外勤拜访
		$this -> display();
	}

//	public function getClient() {
//		$client = A('Waiqin/Index');
//		$clients = $client -> getClient($lng, $lat);
//
//		$this -> ajaxReturn($clients);
//
//	}

	public function signIn() {
		if (IS_AJAX && IS_POST) {
			$data['location_in'] = I('post.address_in');
			$data['longitude_in'] = I('post.lng_in');
			$data['latitude_in'] = I('post.lat_in');
			$data['cid'] = I('post.clientID');
			$data['uid'] = session('userid');
			$data['visit_time'] = strtotime(I('post.visit_time'));
			$data['sign_in_time'] = time();

			$signIn = M('out');
			$signIn -> create();
			$listId = $signIn -> add($data);
			if ($listId) {
				$responseData = array('code' => '200', 'rowId' => $listId, 'create_time' => $data['sign_in_time'], );

			} else {
				$responseData = array('code' => '500');
			}

			$this -> ajaxReturn($responseData);
		}
	}

	public function signOut() {
		if (IS_AJAX && IS_POST && I('post.list_id')){
			$data['location_out'] = I('post.address_out');
			$data['longitude_out'] = I('post.lng_out');
			$data['latitude_out'] = I('post.lat_out');
			$data['appoint_time'] = strtotime(I('post.appoint_time'));
			$data['sign_out_time'] = time();
			$data['id'] = I('post.list_id');
			
			if(!I('post.appoint_time')){
				$this->ajaxReturn(array('code' => 500 , 'mess' => '下次拜访时间为空'));				
			}
			
			$content = I('post.content');
			$i = 0;
			if (is_array($content)) {
				foreach($content as $k => $v){
					if(!trim($content[$k])){
						$i++;
					}else{
					    $contentList[] = array('fid'=>$data['id'],'content'=>$v);
					}
				}
				if($i >0){
					$this->ajaxReturn(array('code' => 500,'mess' => '沟通内容不能为空'));
				}else{
				    // $this->ajaxReturn($contentList);
				    // die;
				    $rel = M('out_content')->addAll($contentList);
				}
			}
			
			//完善签到记录
			
			$signOut = M('out')->save($data);
			if($signOut && $rel){
				$this -> ajaxReturn(array('code' => 200,'mess' => '签退成功')); 
			}
			//$sighOut  -> save($data);
		}else{
			$this->ajaxReturn(array('code' => 500 ,'mess' => '非法的提交途径 OR list_id为空'));
		}
	}

	public function GetClient() {
		$lat = I('post.lat');
		$lon = I('post.lng');
		//$sql = 'select id,client_name,location from ptoa_client where latitude > ' . $lat . '-1 and latitude < ' . $lat . '+1 and longitude > ' . $lon . '-1 and longitude < ' . $lon . '+1 order by ACOS(SIN((' . $lat . ' * 3.1415) / 180 ) *SIN((latitude * 3.1415) / 180 ) +COS((' . $lat . ' * 3.1415) / 180 ) * COS((latitude * 3.1415) / 180 ) *COS((' . $lon . '* 3.1415) / 180 - (longitude * 3.1415) / 180 ) ) * 6380 asc';
		
//		$sql = 'select * from ptoa_client where sqrt(  
//  (  
//   (('.$lon.'-longitude)*PI()*12656*cos((('.$lat.'+latitude)/2)*PI()/180)/180)  
//   *  
//   (('.$lon.'-longitude)*PI()*12656*cos ((('.$lat.'+latitude)/2)*PI()/180)/180)  
//  )  
//  +  
//  (  
//   (('.$lat.'-latitude)*PI()*12656/180)  
//   *  
//   (('.$lat.'-latitude)*PI()*12656/180)  
//  )  
//)< 2';
//echo $sql;
//		$result = M() -> query($sql);
		$result = M('client')->select();
		if(!empty($result)){
			$this -> ajaxReturn($result);			
		}else{
			
		}
		//print_r($result);
		//print_r($result);
		

		//echo $sql;
	}
	

//	public function ajaxlistPage($start,$of){
//		
//	}
	
	
	public function show($id){
		$id = $id;		
		$detail = M('out')->where(array('id' =>$id))->find();
		$content = M('out_content')->where(array('fid' => $id))->select();
		$this->assign('detail',$detail);
		$this->assign('content',$content);
        $this->assign('menu','waiqin'); // 外勤拜访
		$this->display('content');
		
		
	}

}
